﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wicresoft.UnifyShow.Business;
using Oracle.DataAccess.Client;
using System.Data;

namespace Wicresoft.UnifyShow.BusinessImpl
{
    public class Alert : Basic, IAlert
    {
        private const string selectAlertBySiteBSAndDeviceID = "select a.*,d.identify as host from alert a inner join device d on a.DeviceID=d.id " +
"where a.status='OPEN' and d.siteid=:siteid and d.businesssystemid=:businesssystemid";
        private const string selectAlertBySiteAndDeviceID = "select a.*,d.identify as host from alert a inner join device d on a.DeviceID=d.id " +
"where  a.status='OPEN' and  d.siteid=:siteid ";


        public List<AlertDetailAndDevice> GetAlertBySiteBSAndDeviceID(int siteID, int bsID, int? deviceID, out Decimal totalRecords, int pageSize = -1, int pageIndex = -1,
            string sortName = "Timestamp", string sortDirection = "Descending"/*Ascending,Descending*/)
        {
            string sql = selectAlertBySiteBSAndDeviceID;
            List<OracleParameter> paramss = new List<OracleParameter>()
                {
                    new OracleParameter(){ ParameterName="siteid",OracleDbType=OracleDbType.Decimal,Value=siteID},
                     new OracleParameter(){ ParameterName="businesssystemid",OracleDbType=OracleDbType.Decimal,Value=bsID}
                };
            if (deviceID.HasValue)
            {
                sql += " and d.id=:id";
                paramss.Add(
                    new OracleParameter() { ParameterName = "id", OracleDbType = OracleDbType.Decimal, Value = deviceID }
                    );
            }
            var copy = new List<OracleParameter>();
            paramss.ForEach(item => copy.Add((OracleParameter)(item.Clone())));
            totalRecords = ModelHelper<AlertModel>.GetModelCount_By_DirectSql(SQLHelper.GetCountSQLFormDirectSQL(sql), paramss, oda);
            return ModelHelper<AlertDetailAndDevice>.GetModel_By_DirectSql(SQLHelper.GetPageSQLFormDirectSQL(sql, sortName, sortDirection,
                pageSize, pageIndex), copy, oda);
        }

        public List<AlertDetailAndDevice> GetAlertBySiteAndDeviceID(int siteID, int? deviceID, out Decimal totalRecords, int pageSize = -1, int pageIndex = -1,
            string sortName = "Timestamp", string sortDirection = "Descending"/*Ascending,Descending*/)
        {
            string sql = selectAlertBySiteAndDeviceID;
            List<OracleParameter> paramss = new List<OracleParameter>()
                {
                    new OracleParameter(){ ParameterName="siteid",OracleDbType=OracleDbType.Decimal,Value=siteID}
                };
            if (deviceID.HasValue)
            {
                sql += " and d.id=:id";
                paramss.Add(
                    new OracleParameter() { ParameterName = "id", OracleDbType = OracleDbType.Decimal, Value = deviceID }
                    );
            }
            var copy = new List<OracleParameter>();
            paramss.ForEach(item => copy.Add((OracleParameter)(item.Clone())));
            totalRecords = ModelHelper<AlertModel>.GetModelCount_By_DirectSql(SQLHelper.GetCountSQLFormDirectSQL(sql), paramss, oda);
            return ModelHelper<AlertDetailAndDevice>.GetModel_By_DirectSql(SQLHelper.GetPageSQLFormDirectSQL(sql, sortName, sortDirection,
                pageSize, pageIndex), copy, oda);
        }

        public List<AlertModel> GetOpenAlertBySiteID(int site, DateTime? sTime, DateTime? eTime, int? ps, int? pi)
        {
            List<OracleParameter> cmdParms = new List<OracleParameter>();
            string sqlStr = string.Empty;
            string whereStr = string.Empty;

            if (site != null)
            {
                whereStr += " and deviceid in (select id from device where siteid=:siteid) ";
                cmdParms.Add(new OracleParameter("siteid", OracleDbType.Int32, site, ParameterDirection.Input));
            }

            if (sTime != null)
            {
                whereStr += " and timestamp >= :sTime";
                cmdParms.Add(new OracleParameter("sTime", OracleDbType.Date, sTime, ParameterDirection.Input));
            }
            if (eTime != null)
            {
                whereStr += " and timestamp <= :eTime";
                cmdParms.Add(new OracleParameter("eTime", OracleDbType.Date, eTime, ParameterDirection.Input));
            }

            if (ps.HasValue && pi.HasValue)
            {
                int beginIndex = pi.Value * ps.Value;
                int endIndex = (pi.Value + 1) * ps.Value;
                sqlStr = "select * from ( select alert.*,row_number() over ( order by timestamp desc) rn from alert where status =  '" + Alert_Satus.Open + "'" + whereStr + " ) where rn between " + beginIndex + " and " + endIndex;
            }
            else
            {
                sqlStr = "select * from alert where status =  '" + Alert_Satus.Open + "'" + whereStr;
            }
            OracleDataReader dr = oda.ExecuteReader(sqlStr, cmdParms);
            return ModelHelper<AlertModel>.GetModel_By_OracleDataReader(dr);
        }
    }
}
